﻿using System;
using System.Runtime.InteropServices;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 控制台扩展类
    /// </summary>
    public class ConsoleEx
    {
        private const string _DLL = "user32.dll";
        private const string _LP_CLASS_NAME = "ConsoleWindowClass";

        /*
#if CORE7_P
        /// <summary>  
        /// 获取窗口句柄  
        /// </summary>  
        /// <param name="lpClassName"></param>  
        /// <param name="lpWindowName"></param>  
        /// <returns></returns>  
        [LibraryImport(_DLL, SetLastError = true)]
        private static partial IntPtr FindWindow(IntPtr lpClassName, IntPtr lpWindowName);

        /// <summary>  
        /// 设置窗体的显示与隐藏  
        /// </summary>  
        /// <param name="hWnd">窗口句柄</param>  
        /// <param name="nCmdShow">显示或隐藏标识[0:隐藏;1:显示]</param>  
        /// <returns>执行结果</returns>  

        [LibraryImport(_DLL, SetLastError = true)]
        private static partial int ShowWindow(IntPtr hWnd, uint nCmdShow);
#else

#endif
*/
        /// <summary>  
        /// 获取窗口句柄  
        /// </summary>  
        /// <param name="lpClassName"></param>  
        /// <param name="lpWindowName"></param>  
        /// <returns></returns>  
        [DllImport(_DLL, SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>  
        /// 设置窗体的显示与隐藏  
        /// </summary>  
        /// <param name="hWnd">窗口句柄</param>  
        /// <param name="nCmdShow">显示或隐藏标识[0:隐藏;1:显示]</param>  
        /// <returns>执行结果</returns>  
        [DllImport(_DLL, SetLastError = true)]
        private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);




        /// <summary>
        /// 隐藏当前控制台
        /// </summary>
        public static void HideCucrrentConsole()
        {
            if (!EnvironmentEx.IsWindowPlatform())
            {
                return;
            }

            // Disable the warning.
#pragma warning disable CA1416
            IntPtr hWnd = FindWindow(_LP_CLASS_NAME, Console.Title);

            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 0);
            }
            // Re-enable the warning.
#pragma warning restore CA1416
        }

        /// <summary>
        /// 隐藏指定控制台标题的控制台
        /// </summary>
        /// <param name="consoleTitle">控制台标题,不能为空字符串或全空格或null</param>
        //[SupportedOSPlatform("windows")]
        public static void HideConsole(string consoleTitle)
        {
            if (!EnvironmentEx.IsWindowPlatform())
            {
                return;
            }

            if (string.IsNullOrEmpty(consoleTitle))
            {
                throw new ArgumentNullException(nameof(consoleTitle));
            }

            IntPtr hWnd = FindWindow(_LP_CLASS_NAME, consoleTitle);
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 0);
            }
        }







        /// <summary>
        /// 显示当前控制台
        /// </summary>
        public static void ShowCucrrentConsole()
        {
            if (!EnvironmentEx.IsWindowPlatform())
            {
                return;
            }

            // Disable the warning.
#pragma warning disable CA1416
            IntPtr hWnd = FindWindow(_LP_CLASS_NAME, Console.Title);
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 1);
            }
            // Re-enable the warning.
#pragma warning restore CA1416
        }

        /// <summary>
        /// 显示指定控制台标题的控制台
        /// </summary>
        /// <param name="consoleTitle">控制台标题,不能为空字符串或全空格或null</param>
        public static void ShowConsole(string consoleTitle)
        {
            if (!EnvironmentEx.IsWindowPlatform())
            {
                return;
            }

            if (string.IsNullOrEmpty(consoleTitle))
            {
                throw new ArgumentNullException(nameof(consoleTitle));
            }

            IntPtr hWnd = FindWindow(_LP_CLASS_NAME, consoleTitle);
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 1);
            }
        }
    }
}